﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Viewer.BusinessObject.BusinessEntities;

namespace Viewer.ToChuc.UserControl
{
    public partial class UCLookUpToChucHanhChinh : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadDataForTree();
            }
        }

        private void LoadDataForTree()
        {
           //Load Root  
            Ext.Net.TreeNode root = new Ext.Net.TreeNode("0","Tổ Chức Hành Chính",Icon.Folder);
            root.Expanded = true;
            root.Disabled = true;
           
            treeToChucHanhChinh.Root.Add(root);

            List<CHToChucHanhChinh> lstToChuc =(new CHToChucHanhChinh()).GetAll();
            
            

            var lstToChucLv0 = (from m in lstToChuc
                            where m.ToChucHanhChinhLevel == 0
                            select m).ToList();

            foreach (var oToChucLv0 in lstToChucLv0)
            {
                var nodeLv0 = CreateNode(oToChucLv0);
                root.Nodes.Add(nodeLv0);

                List<CHToChucHanhChinh> lstToChucLv1 = lstToChuc.Where(c => c.ToChucHanhChinhIDCha == oToChucLv0.ToChucHanhChinhID).ToList();

                foreach (var oToChucLv1 in lstToChucLv1)
                {
                    var nodeLv1 = CreateNode(oToChucLv1);
                    nodeLv0.Nodes.Add(nodeLv1);
                    List<CHToChucHanhChinh> lstToChucLv2 = lstToChuc.Where(c => c.ToChucHanhChinhIDCha == oToChucLv1.ToChucHanhChinhID).ToList();
                    foreach (var oToChucLv2 in lstToChucLv2)
                    {
                        var nodeLv2 = CreateNode(oToChucLv2);
                        nodeLv1.Nodes.Add(nodeLv2);
                        List<CHToChucHanhChinh> lstToChucLv3 = lstToChuc.Where(c => c.ToChucHanhChinhIDCha == oToChucLv2.ToChucHanhChinhID).ToList();
                        foreach (var oToChucLv3 in lstToChucLv3)
                        {
                            var nodeLv3 = CreateNode(oToChucLv3);
                            nodeLv2.Nodes.Add(nodeLv3);
                        }
                    }
                }
            }
            if (X.IsAjaxRequest)
            {
                Ext.Net.X.AddScript("refreshTree({0},{1});", treeToChucHanhChinh.ClientID, treeToChucHanhChinh.Root.ToJson());
            }
        }

        private Ext.Net.TreeNode CreateNode(CHToChucHanhChinh oToChuc)
        {
            Ext.Net.TreeNode node = new Ext.Net.TreeNode(oToChuc.ToChucHanhChinhID.ToString(), oToChuc.TenToChucHanhChinh, Icon.Group);
            node.Expanded = false ;
            return node;
        }

        public void ShowDialog()
        {
            //this.btnAccept.Disabled = true;
            //treeToChucHanhChinh.SelectionModel.Clear();
            wdLookUpToChucHanhChinh.Show();
        }
    }
}